package com.example.demozy.mapper;

import com.example.demozy.entity.RealNameUser;
import com.example.demozy.entity.User;
import com.example.demozy.entity.UserBasicInformation;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author 王成龙
 */
@Mapper
public interface UserMapper {

    /**
     * 登录，获取用户列表
     *
     * @param account
     * @param userPwd
     */
    @Select("select * " + "from user " +
            "where account = #{account} and user_pwd = #{userPwd}")
    List<User> listUsers(@Param("account") String account,
            @Param("userPwd") String userPwd);

    /**
     * 插入用户表
     *
     * @param account
     * @param userPwd
     * @param mobile
     */
    @Insert("insert into user " + "values(0,#{account},#{userPwd},#{mobile}," +
            "null,0,0,0,now())")
    int insert(@Param("account") String account,
            @Param("userPwd") String userPwd, @Param("mobile") String mobile);

    /**
     * 新增基本信息表记录
     * @param userId
     * @return
     */
    @Insert("insert into basic_information(user_id,update_time) values(#{user_id},now())")
    int insertBasicInformation(int userId);

    /**
     * 获取user表最新的user_id
     */
    @Select("select max(user_id) " + "from user")
    int getMaxUserId();

    /**
     * 根据user_id获取用户
     *
     * @param userId
     * @return
     */
    @Select("select * " + "from user " + "where user_id = #{userId}")
    User getUserByUserId(int userId);

    /**
     * 判断账号是否重复
     *
     * @param account
     * @return
     */
    @Select("select account from user where account = #{account}")
    String getAccountByAccount(String account);

    /**
     * 判断手机号是否重复
     *
     * @param mobile
     * @return
     */
    @Select("select mobile from user where mobile = #{mobile}")
    String getMobileByMobile(String mobile);

    /**
     * 根据userid获取用户基本信息
     *
     * @param userId
     * @return
     */
    @Select("select * from basic_information where user_id = #{userId}")
    UserBasicInformation getBasicInfoById(int userId);

    /**
     * 根据userid获取实名用户
     *
     * @param userId
     * @return
     */
    @Select("select * from real_name_user where user_id = #{userId}")
    RealNameUser getRealNameUserById(int userId);

    /**
     * 修改基本资料
     *
     * @param userId
     * @param nickname
     * @param userDescription
     * @return
     */
    @Update("update basic_information set nickname = #{nickname}," +
            "user_description = " +
            "#{userDescription} where user_id = #{userId}")
    int updateBasicInfo(@Param("userId") int userId,
            @Param("nickname") String nickname,
            @Param("userDescription") String userDescription);

    /**
     * 根据用户id 修改邮箱
     * @param userId
     * @param email
     * @return
     */
    @Update("update user set email = #{email} where user_id = #{userId}")
    int updateEmailById(@Param("userId") int userId, @Param("email") String email);

    /**
     * 插入根据身份证号获得的性别和出生日期
     * @param userId
     * @param gender
     * @param birthday
     * @return
     */
    @Update("update basic_information set gender = #{gender},birthday = " +
            "#{birthday} where user_id = #{userId}")
    int updateGenderAndBirthday(@Param("userId") int userId,
            @Param("gender") String gender, @Param("birthday") Date birthday);

    /**
     * 根据用户id获取实名状态
     * @param userId
     * @return
     */
    @Select("select real_stat from user where user_id = #{userId}")
    int getRealStatByUserId(int userId);

    /**
     * 插入实名认证信息
     *
     * @param userId
     * @param realName
     * @param idCardNumber
     * @return
     */
    @Insert("insert into real_name_user values(#{userId},#{realName}," +
            "#{idCardNumber},now())")
    int insertRealNameUser(@Param("userId") int userId,
            @Param("realName") String realName,
            @Param("idCardNumber") String idCardNumber);

    /**
     * 插入钱包表
     *
     * @param userId
     */
    @Insert("insert into user_wallet values(#{userId}, 10000, #{userId}, now())")
    void insertUserWalletByUserId(@Param("userId")int userId);


    /**
     * 根据用户id更新用户表实名认证信息
     *
     * @param userId
     * @return
     */
    @Update("update user set real_stat = 1 where user_id = #{userId}")
    int updateRealStatById(int userId);

    /**
     * 根据身份证信息返回实名用户列表
     *
     * @param idCardNumber
     * @return
     */
    @Select("select * from real_name_user where id_card_number = " +
            "#{idCardNumber}")
    List<RealNameUser> listRealNameUsersByIdNo(String idCardNumber);
}
